/*本模块功能:教师虚机数量设置
        编写者:杨锡君
        编写时间:2018.08.08
        */
$(function(){
    var Alert1 = new Alert;
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    var index={
        init:function(){
            this.bind();
            maxLengthLimit.init("publicText");
        },
        bind:function(){
            var self=this;
            self.load();
            $("#num").off("blur").blur(function () {
                var numVal = $("#num").val();
                if(Number(numVal) == 0){
                    $("#num").val(0);
                }
            });
            $(".saveVirNumBtn").off("click").click(function(){//点击提交
                var numVal = $("#num").val();
                var reg = /^\d+$/;
                if(reg.test(numVal)){
                    if(Number(numVal)<100){
                        self.delivery();
                    }else{
                        Alert1.content = "请正确输入教师镜像保存数量";
                        Alert1.init();
                        Alert1.show();
                    }
                }else{
                    Alert1.content = "请正确输入教师镜像保存数量";
                    Alert1.init();
                    Alert1.show();
                }

            });

        },
        load:function(){//加载页面基本信息
            $.ajax({
                type: "get",
                url: "manage/config/query",
                async: true,
                success: function(msg) {
                    if(msg.code == 200){
                        var msgNew = msg.data;
                        $("#num").val(msgNew.imageQuota);
                    }
                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },
        delivery:function(){//点击提交
            loading1.show();
            var json = JSON.stringify({"imageQuota":Number($("#num").val())});
            $.ajax({
                type: "post",
                url: "manage/config/update",
                async: true,
                dataType:'JSON',
                contentType:'application/json',
                data:json,
                success: function(msg) {
                    loading1.hide();
                    if(msg.code == 200){
                        Alert1.content = "设置成功";
                        Alert1.init();
                        Alert1.show();
                    }else{
                        Alert1.content = msg.message;
                        Alert1.init();
                        Alert1.show();
                    }
                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        }

    };
    index.init();
});
